﻿using System;
using Microsoft.ApplicationServer.Caching;

namespace Popw.LiveBlog.Repositories.Cache
{
	public class AzureCacheProvider : ICacheProvider
	{
		public AzureCacheProvider()
		{
			_cache = new DataCache("default");
		}

		private readonly DataCache _cache;

		public void Add(string key, object item, int duration)
		{
			_cache.Put(key, item, new TimeSpan(0, 0, 0, 0, duration));
		}

		public T Get<T>(string key) where T : class
		{
			return _cache.Get(key) as T;
		}

		public T? GetValue<T>(string key) where T : struct
		{
			var cachedObject = _cache.Get(key);
			if (cachedObject == null)
				return null;
			return (T)cachedObject;
		}

		public void Remove(string key)
		{
			_cache.Remove(key);
		}

		public void Clear()
		{
		} 
	}
}